import config from "@/config/index.js";
import * as imageConversion from 'image-conversion'
import fly from "flyio"

export default function(callback) {
	let promise;
	if (typeof callback !== 'function') {
		promise = new Promise((resolve, reject) => {
			callback = function(resolvedata, rejectdata) {
				if (resolvedata) {
					resolve(resolvedata);
				} else {
					reject(rejectdata);
				}
			};
		});
	}
	
	uni.chooseImage({
		success: (chooseImageRes) => {
			const data = chooseImageRes.tempFiles[0];
			imageConversion
				.compressAccurately(data, 100)
				.then(async (res) => {
					let formData = new FormData()
					formData.append('pic', res, name.name)
					uni.showLoading({
						title: '加载中'
					});
					let reqdata = await fly.post(config.baseURL + "/UploadGetVin", formData, {
						headers: {
							"content-type": "multipart/form-data"
						}
					})
					uni.hideLoading()
					callback(JSON.parse(reqdata.data))
				})

		}
	});

	return promise;
}